package main;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsLineItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QLineF;
import com.trolltech.qt.gui.QPen;
import com.trolltech.qt.gui.QGraphicsItem.GraphicsItemFlag;

public class QInheritArrow extends QCustomArrow {
	
	public QInheritArrow (QGraphicsClass parent, QGraphicsClass child, QGraphicsScene scene){
		super(parent, child, scene);
	}
	
	public void display(){
		_start = getStartPoint();
		_end = getEndPoint();
		if(!middleHasMoved){
			_middle = calculateMiddlePoint();
		}
		
		// Dispose of all the lines that are already displayed
		for(QGraphicsLineItem line : lines){
			line.dispose();
		}
		lines.clear();
		
		lines.add(drawLine(_start, _middle));
		
		if(!middleHasMoved){
			if(middlePoint!=null){
				middlePoint.dispose();
			}
			middlePoint = new QGraphicsMiddlePoint(null, _scene);
			middlePoint.setPos(_middle.x()-4, _middle.y()-4);
			middlePoint.setFlag(GraphicsItemFlag.ItemIsMovable);
			middlePoint.setFlag(GraphicsItemFlag.ItemIsFocusable);
			middlePoint.show();
		}
		
		lines.add(drawLine(_middle, _end));
		drawArrowHead(_end.x(),_end.y(), _middle.x(), _middle.y());
	}
	
	public void drawArrowHead(double px, double py, double cx, double cy){
		// x y  represent the coordinates of the point that will be transformed
		double x,y;
		QLineF unitVector = (new QLineF(new QPointF(px, py), new QPointF(cx, cy))).unitVector();
		// (x, y) is a point of the line between (cx, cy) and (px, py) that is exactly 12 units away from (px, py)
		// in the direction of (cx, cy)
		x = px + 12*unitVector.dx();
		y =py + 12*unitVector.dy();

		QLineF leftPart = new QLineF(new QPointF(px, py), new QPointF(x,y));			
		
		QGraphicsLineItem gLeftPart = new QGraphicsLineItem(leftPart, null , _scene);
		gLeftPart.translate(px, py);
		gLeftPart.rotate(30);
		gLeftPart.translate(-px, -py);
		
		gLeftPart.setPen(new QPen(new QColor("black"), 1 ));
		gLeftPart.show();
		
		lines.add(gLeftPart);
		

		QGraphicsLineItem gRightPart = new QGraphicsLineItem(leftPart, null , _scene);
		gRightPart.translate(px, py);
		gRightPart.rotate(-30);
		gRightPart.translate(-px, -py);
		gRightPart.setPen(new QPen(new QColor("black"), 1 ));
		gRightPart.show();
		
		lines.add(gRightPart);
	}
	
	// Get the starting point of the arrow (i.e. not the arrowhead side)
	protected QPointF getStartPoint(){
		// Calculate the positions on the scene of the points that need to be linked
		QPointF childTL, childTR;
		double cx, cy;
		
		childTL = _child.mapToScene(_child.boundingRect().topLeft());
		childTR = _child.mapToScene(_child.boundingRect().topRight());
		
		cx = (childTL.x()+childTR.x())/2;
		cy = childTL.y();
		
		return new QPointF(cx,cy);
	}
	
	// Get the end point of the arrow (i. e. the tip of the arrowhead)
	protected QPointF getEndPoint(){
		QPointF parentBL, parentBR;
		double px, py;
		
		parentBL = _parent.mapToScene(_parent.boundingRect().bottomLeft());
		parentBR = _parent.mapToScene(_parent.boundingRect().bottomRight());
		
		px = (parentBL.x()+parentBR.x())/2;
		py = parentBL.y();
		
		
		return new QPointF(px, py);
	}
}
